//deck.cpp
#include "deck.h"
//need random number generator

//constructor  
// makes a clean new deck of cards in order
Deck::Deck(){
  //cout<<"before setting index"<<endl;
  deckIndex = 0;
  //cout<<"before new deck loop"<<endl;
  for(int i=1;i<14;i++){
    //cout<<"value loop"<<endl;
    for(int j=1;j<5;j++){
      //cout << "suit loop" << endl;
      //cout << "Deck Index: " << deckIndex << endl;
      //cout << "Value: " << i << endl;
      //cout << "Suit: " << Cards::Suit(j) << endl;
      Cards* insertedCard;
      insertedCard = new Cards(i,Cards::Suit(j));
      thisDeck[deckIndex] = insertedCard; //Cards::Cards(i,Cards::Suit(j));
      deckIndex++;
    }
  }
  //shuffles the new deck
  Shuffle();
}

Deck::Deck(int newDeck){
  deckIndex = 0;

}
/*Deck::~Deck()
{
  Cards* beingDeleted;
  while(deckIndex > 0)
    {
      beingDeleted = thisDeck[deckIndex-1];
      thisDeck[deckIndex-1] = NULL;
      delete beingDeleted;
      deckIndex--;
    }
}
*/

void Deck::Shuffle(){
  if (deckIndex > 1)
{
  cout << "initalizing shuffling\n";
  int i = deckIndex;

  Deck tempDeck(i);  //throwing in i as an int to not make a new sorted deck, doesnt do anything
  
  for (i = deckIndex; i != 0; i--) //this 'for' block fills tempDeck in a random order
    {      
      Cards* testCard = DrawCard();
      //cout << "Inserting card: V:" << testCard->GetValue() << " S:" << testCard->GetSuit() << endl;
      tempDeck.InsertRandomly(testCard); 
      //tempDeck.PrintDeck();
      //cout << "\nlol\n";
    }
  while (tempDeck.deckIndex != 0) //this 'for' block refills this deck with tempDeck's cards
    {
      InsertRandomly(tempDeck.DrawCard());
    }
 }
}

Cards* Deck::DrawCard(){
  Cards* returnee = thisDeck[deckIndex - 1];
  //cout << "Drawing card, V:" << returnee->GetValue() << " S:"  << returnee->GetSuit() << endl;
  thisDeck[deckIndex - 1] = NULL;
  deckIndex--;
  return returnee;
}

void Deck::InsertRandomly(Cards* cardIn){
  //cout << "test\n";
  if (deckIndex == 0)
    {
      //      cout << "test1\n";
      //      cout << "deckIndex = 0, card is " << cardIn->GetValue() << " " << cardIn->GetSuit() << endl;
      thisDeck[0] = cardIn;
      deckIndex++;
    }
  else
    {
      //  cout << "test2 deckIndex = " << deckIndex << endl;
      // cout << "deckIndex = " << deckIndex << ", card is " << cardIn->GetValue() << " " << cardIn->GetSuit() << endl;
      int placement = rand() % deckIndex + 1;
      Cards* Next;
      while (placement < deckIndex + 1)
	{
	  Next = thisDeck[placement];
	  thisDeck[placement] = cardIn;
	  cardIn = Next;
	  placement++;
	}
      deckIndex ++;
    }
}

void Deck::PrintDeck(){

  int index = 0;
  /*
  while ( index < deckIndex)
    {
      cout << thisDeck[index]->GetValue() << " " << thisDeck[index]->GetSuit() << "\n";
      index++;
    }
 */
    while ( index < deckIndex)
    {
      /*
      cout << thisDeck[index]->ValueString()<<" of ";
      cout << thisDeck[index]->StringSuit() << endl;
      */
      cout<<index+1<<": ";
      cout << thisDeck[index]->StringCard()<<endl;
      index++;
      #ifdef _MAINWINDOW_H
      //frame->printcard()
      #endif
    }



  

}

int Deck::Size() const{
  return deckIndex;
}

void Deck::InsertAtBottom(Cards* oneCard){
  if (deckIndex == 0) //checks if deck is empty
    {
      thisDeck[0] = oneCard;
      deckIndex++;
    }
  else
    {
      int placement = 0;
      Cards* next;
      while(placement < deckIndex + 1)
	{
	  next = thisDeck[placement];
	  thisDeck[placement] = oneCard;
	  oneCard = next;
	  placement++;
	}
      deckIndex++;
    }
}
